<?php


namespace app\api\model\ShopGoods;



use think\facade\Db;
use think\Model;

class ShopGoodsModel extends Model
{
    /**
     * 数据表名
     * @var string
     */
    protected $name = "shop_goods";

    /**
     * 根据条件查询单条数据
     * @param $where
     * @param string $field
     * @return mixed
     * @throws
     */
    public static function getGoodsWhere($where,$field="*")
    {
        $goods = Db::name("shop_goods")->field($field)->where($where)->find();
        return $goods;
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getGoodsWhereMore($where,$filed="*")
    {
        $list = Db::name("shop_goods")->field($filed)->where($where)->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getGoodsCountWhere($where)
    {
        $count = Db::name("shop_goods")->where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param array $where
     * @param int $sort
     * @param string $field
     * @return array
     * @throws
     */
    public static function getGoodsListWhere($page,$limit,$where = [],$sort=0,$field="g.*")
    {
        $start = ceil(($page-1)*$limit);
        $model = Db::name('shop_goods')
            ->alias('g')
            ->join('shop_goods_cate_relation gcr','gcr.goods_id = g.id')
            ->field($field)
            ->where($where);
        if(!empty($sort)){
            if($sort == 1){
                $model->order('g.price desc');
            }
            if($sort == 2){
                $model->order('g.price asc');
            }
            if($sort == 3){
                $model->order('g.sales desc');
            }
            if($sort == 4){
                $model->order('g.sales asc');
            }
        }else{
            $model->order('g.sort desc');
        }
        $list = $model->limit($start,$limit)
            ->select()->toArray();
        return $list;
    }

    /**
     * 自增
     * @param $uid
     * @param $field
     * @param $amount
     * @return int
     * @throws
     */
    public static function updateGoodsInc($goods_id,$field,$amount)
    {
        $flag = Db::name('shop_goods')->where([['id','=',$goods_id]])->inc($field,$amount)->update();
        return $flag;
    }

    /**
     * 自减
     * @param $uid
     * @param $field
     * @param $amount
     * @return int
     * @throws
     */
    public static function updateGoodsDec($goods_id,$field,$amount)
    {
        $flag = Db::name('shop_goods')->where([['id','=',$goods_id]])->dec($field,$amount)->update();
        return $flag;
    }

    /**
     * 商品&销量变化
     * @param $goods_id
     * @param $num
     * @return bool
     * @throws
     */
    public static function goodsInOut($goods_id, $num)
    {
        $flag = Db::name('shop_goods')->where([['id','=',$goods_id]])->inc('sales', $num)->update();
        if (!$flag) {
            return false;
        }
        $flag = Db::name('shop_goods')->where([['id','=',$goods_id]])->dec('stock', $num)->update();
        if (!$flag) {
            return false;
        }
        return true;
    }

    /**
     * 获取随机几条数据
     * @param $num
     * @param $where
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public static function getRandomGoods($num,$where){
        $list = Db::name('shop_goods')->where($where)->count();
        $min = Db::name('shop_goods')->where($where)->min('id');
        $max = $min+$list;
        if($list < $num){$num = $list;}
        $i = 1;
        $flag = 0;
        $ary = array();
        while($i<=$num){
            $rundnum = rand($min, $max);
            if($flag != $rundnum){
                if(!in_array($rundnum,$ary)){
                    $ary[] = $rundnum;
                    $flag = $rundnum;
                }else{
                    $i--;
                }
                $i++;
            }
        }
        $data = Db::name('shop_goods')->where('id','in',$ary,'or')->select()->toArray();
        return $data;
    }

}
